package com.dissertation.service.impl;

import com.dissertation.model.ApplyAllow;
import com.dissertation.model.Assets;
import com.dissertation.pojo.Message;
import com.dissertation.respository.ApplyAllowRepository;
import com.dissertation.respository.AssetsRepository;
import com.dissertation.service.IApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ApplyServiceImpl implements IApplyService {


    @Autowired
    ApplyAllowRepository applyAllowRepository;

    @Autowired
    AssetsRepository assetsRepository;

    @Override
    public Message addApply(ApplyAllow applyAllow) {
        ApplyAllow temp = applyAllowRepository.findByAssetIdAndApplyNameAndDealIsFalse(applyAllow.getAssetId(),applyAllow.getApplyName());
        if(temp!=null){
            return Message.createErr("您已经申请了，请勿重复申请,请等待第一负责人进行处理");
        }
        Optional<Assets> assets = Optional.ofNullable(assetsRepository.findOne(applyAllow.getAssetId()));
        assets.ifPresent(e-> applyAllow.setOwner(e.getAuthor()));
        applyAllow.setDeal(false);
        applyAllow.setStatus(false);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(new Date());
        applyAllow.setTime(time);
        return Message.createSuc(applyAllowRepository.save(applyAllow));
    }

    @Override
    public Message alterApply(String id,boolean result,Integer valid) {
        ApplyAllow applyAllow = applyAllowRepository.findOne(id);
        if(applyAllow!=null){
            applyAllow.setDeal(true);
            applyAllow.setStatus(result);
            if(valid!=null){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar calendar = Calendar.getInstance();
                applyAllow.setValid(valid);
                if(valid==0){
                    calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)+10);
                    String deadtime = format.format(calendar.getTime());
                    applyAllow.setDeadtime(deadtime);
                }else{
                    calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY)+valid);
                    applyAllow.setDeadtime(format.format(calendar.getTime()));
                }
            }
            applyAllowRepository.save(applyAllow);
            return Message.createSuc("处理成功");
        }else{
            return Message.createErr("id错误");
        }
    }

    @Override
    public Message deleApply(String[] ids) {
        List<ApplyAllow> applyAllowList = applyAllowRepository.findByIdIn(ids);
        if(applyAllowList!=null){
            applyAllowRepository.delete(applyAllowList);
            return Message.createSuc("删除成功");
        }else{
            return Message.createErr("id错误");
        }
    }
}
